﻿using System;
using System.Configuration;
using System.Data.Common;
using Dataglot;
using IrishSettings.TableControllers;
using NUnit.Framework;

namespace IrishSettings.IntegrationTests.TableControllerTests
{
    internal class ConfigUtils
    {
        public static DataFactory DefaultDataFactory()
        {
            DataFactory factory = new DataFactory(ConfigurationManager.AppSettings["Dataglot.ConnectionStringName"]);
            return factory;
        }

        public static string ConnectionString()
        {
            string name = ConfigurationManager.AppSettings["Dataglot.ConnectionStringName"];
            return ConfigurationManager.ConnectionStrings[name].ConnectionString;//OledbSql
        }

        public static string Provider()
        {
            string name = ConfigurationManager.AppSettings["Dataglot.ConnectionStringName"];
            return ConfigurationManager.ConnectionStrings[name].ProviderName;//OledbSql
        }

        public static string OleDbConnectionString()
        {
            string name = ConfigurationManager.AppSettings["Dataglot.ConnectionStringName"];
            return ConfigurationManager.ConnectionStrings[name].ConnectionString;//OledbSql
        }
    }

    [TestFixture]
    public class SettingsSchemaControllerTests
    {
        
        [Test]
        public void Select()
        {
            DataFactory factory = new DataFactory(ConfigUtils.OleDbConnectionString(), ConfigUtils.Provider());
            using (DbConnection con = factory.Connection())
            {
                TransactionManager manager = new TransactionManager(con);
                manager.Begin();
                ApplicationAndVersionController applicationController = new ApplicationAndVersionController(factory, con, manager.Transaction);
                SettingsApplicationStored app =  applicationController.Insert(CreateASampleAppliction());
                SchemaSettingsController c = new SchemaSettingsController(factory, con, manager.Transaction);

                GenericSettingSchema schemaItem= c.Insert(CreateASampleSchema(app.Id));
                c.Select(schemaItem.Id);
            }
        }

        [Test]
        public void Insert()
        {
            DataFactory factory = new DataFactory(ConfigUtils.OleDbConnectionString(), ConfigUtils.Provider());
            using (DbConnection con = factory.Connection())
            {
                TransactionManager manager = new TransactionManager(con);
                manager.Begin();
                ApplicationAndVersionController applicationController = new ApplicationAndVersionController(factory, con, manager.Transaction);
                SettingsApplicationStored app = applicationController.Insert(CreateASampleAppliction());
                SchemaSettingsController c = new SchemaSettingsController(factory, con, manager.Transaction);

                GenericSettingSchemaStored schemaItem = c.Insert(CreateASampleSchema(app.Id));
                Assert.IsNotNull(schemaItem);
            }
        }

        [Test]
        public void Update()
        {
            DataFactory factory = new DataFactory(ConfigUtils.OleDbConnectionString(), ConfigUtils.Provider());
            using (DbConnection con = factory.Connection())
            {
                TransactionManager manager = new TransactionManager(con);
                manager.Begin();
                ApplicationAndVersionController applicationController = new ApplicationAndVersionController(factory, con, manager.Transaction);
                SettingsApplicationStored app = applicationController.Insert(CreateASampleAppliction());
                SchemaSettingsController c = new SchemaSettingsController(factory, con, manager.Transaction);

                GenericSettingSchema schemaItem = c.Insert(CreateASampleSchema(app.Id));
                schemaItem.Name = "Blah"; 
                c.Update(schemaItem);
            }
        }

        [Test]
        public void Delete()
        {
            DataFactory factory = new DataFactory(ConfigUtils.OleDbConnectionString(), ConfigUtils.Provider());
            using (DbConnection con = factory.Connection())
            {
                TransactionManager manager = new TransactionManager(con);
                manager.Begin();
                ApplicationAndVersionController applicationController = new ApplicationAndVersionController(factory, con, manager.Transaction);
                SettingsApplicationStored app = applicationController.Insert(CreateASampleAppliction());
                SchemaSettingsController c = new SchemaSettingsController(factory, con, manager.Transaction);

                GenericSettingSchemaStored schemaItem = c.Insert(CreateASampleSchema(app.Id));

                c.Delete(schemaItem.Id);
            }
        }

        [Test]
        public void DoesNotExist()
        {
            DataFactory factory = new DataFactory(ConfigUtils.OleDbConnectionString(), ConfigUtils.Provider());
            using (DbConnection con = factory.Connection())
            {
                TransactionManager manager = new TransactionManager(con);
                manager.Begin();

                SchemaSettingsController c = new SchemaSettingsController(factory, con, manager.Transaction);

               Assert.IsFalse(c.Exists(99999999));
            }
        }

        [Test]
        public void Exists()
        {
            DataFactory factory = new DataFactory(ConfigUtils.OleDbConnectionString(), ConfigUtils.Provider());
            using (DbConnection con = factory.Connection())
            {
                TransactionManager manager = new TransactionManager(con);
                manager.Begin();
                ApplicationAndVersionController applicationController = new ApplicationAndVersionController(factory, con, manager.Transaction);
                SettingsApplicationStored app = applicationController.Insert(CreateASampleAppliction());
                SchemaSettingsController c = new SchemaSettingsController(factory, con, manager.Transaction);

                GenericSettingSchemaStored schemaItem = c.Insert(CreateASampleSchema(app.Id));

                Assert.IsTrue( c.Exists(schemaItem.Id));
            }
        }


        private SettingsApplication CreateASampleAppliction()
        {
            SettingsApplication setting = new SettingsApplication();
            setting.Application = "IntegrationTests";
            setting.Culture = "en-US";
            setting.Version = "1.0";
            setting.CurrentVersion = "1.0";
            return setting;
        }

        private GenericSettingSchema CreateASampleSchema(long applicationId)
        {
            if (applicationId == 0)
                throw new InvalidOperationException("app id required");

            GenericSettingSchema setting = new GenericSettingSchema();
            setting.ApplicationId = applicationId;
            setting.DefaultSerializedValue = "6";
            setting.ValueType = "System.String";
            setting.Scope = SettingScope.Application;
            setting.IsUser = false;
            setting.IsAnonymous = false;
            return setting;
        }
    }
}
